using System;
using UnityEngine;

public class Steering : MonoBehaviour
{
	public static Vector2 Seek(Vector2 direction, Vector2 target, Vector2 position, float speed)
	{
		Vector2 vector = target - position;
		vector.Normalize();
		vector *= speed;
		return vector - direction * speed;
	}

	public static Vector2 SeekTheCenterOfTheMap(Vector2 direction, Vector2 position, float speed)
	{
		Vector2 vector = Vector2.zero - position;
		vector.Normalize();
		vector *= speed;
		return vector - direction * speed;
	}

	public static Vector2 Flee(Vector2 direction, Vector2 target, Vector2 position, float speed)
	{
		Vector2 vector = position - target;
		vector.Normalize();
		vector *= speed;
		return vector - direction * speed;
	}

	public static Vector2 Wander(Vector2 direction, Vector2 position, float speed, float AICircleDistance, float AICircleRadius, float AngleChange, ref Vector2 displacement)
	{
		if (displacement == Vector2.zero)
		{
			displacement = UnityEngine.Random.insideUnitCircle;
			displacement *= AICircleRadius;
		}
		else
		{
			displacement = Rotate(displacement, UnityEngine.Random.Range(0f, 1f) * AngleChange - AngleChange * 0.5f);
			displacement *= AICircleRadius;
		}
		Vector2 vector = direction;
		vector *= AICircleDistance;
		return vector + displacement;
	}

	public static Vector2 Pursuit(Vector2 direction, Vector2 target, Vector2 position, float mySpeed, float targetSpeed, Vector2 targetDirection)
	{
		float num = Vector2.Distance(target, position) / mySpeed * Time.deltaTime;
		Vector2 target2 = target + targetDirection * targetSpeed * Time.deltaTime * num;
		return Seek(direction, target2, position, mySpeed);
	}

	public static Vector2 Evade(Vector2 direction, Vector2 target, Vector2 position, float speed, Vector2 targetDirection, float targetSpeed)
	{
		float num = Vector2.Distance(target, position) / speed * Time.deltaTime;
		Vector2 target2 = target + targetDirection * targetSpeed * Time.deltaTime * num;
		return Flee(direction, target2, position, speed);
	}

	public static Vector2 AvoidCollision(Collider2D obstacle, Vector2 direction, float speed, Vector2 position)
	{
		Vector2 result = Vector2.zero;
		if (obstacle != null)
		{
			Vector2 vector = obstacle.bounds.ClosestPoint(position);
			result = Flee(direction, vector, position, speed);
			result /= (vector - position).sqrMagnitude;
			result *= Values.Instance.AIAvoidCollisionForce;
		}
		return result;
	}

	public static Vector2 SetAngle(Vector2 vec, float value)
	{
		float magnitude = vec.magnitude;
		vec.x = Mathf.Cos(value) * magnitude;
		vec.y = Mathf.Sin(value) * magnitude;
		return vec;
	}

	public static Vector2 RunAwayFromBorder(Vector2 direction, Vector2 position, float speed)
	{
		Vector2 result = Vector2.zero;
		Vector2 vector = position;
		bool flag = false;
		if (position.x + 1f > (float)Values.Instance.AreaSize)
		{
			flag = true;
			position.x += 1f;
		}
		else if (position.x - 1f < 0f)
		{
			flag = true;
			position.x -= 1f;
		}
		if (position.y + 1f > (float)Values.Instance.AreaSize)
		{
			flag = true;
			position.y += 1f;
		}
		else if (position.y - 1f < 0f)
		{
			flag = true;
			position.y -= 1f;
		}
		if (flag)
		{
			result = Flee(direction, vector, position, speed);
			result /= (vector - position).sqrMagnitude;
			result *= Values.Instance.AIAvoidCollisionForce;
		}
		return result;
	}

	private static Vector2 Rotate(Vector2 rotationVector, float degrees)
	{
		float num = Mathf.Sin(degrees * ((float)Math.PI / 180f));
		float num2 = Mathf.Cos(degrees * ((float)Math.PI / 180f));
		float x = rotationVector.x;
		float y = rotationVector.y;
		rotationVector.x = num2 * x - num * y;
		rotationVector.y = num * x + num2 * y;
		return rotationVector;
	}
}
